package com.dengc.controller;

import com.dengc.pojo.GoodsStorageRecord;
import com.dengc.pojo.GoodsSupplier;
import com.dengc.pojo.SysRole;
import com.dengc.pojo.SysUser;
import com.dengc.service.GoodsStorageRecordService;
import com.dengc.service.GoodsSupplierService;
import com.dengc.utils.Contants;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class GoodsStorageRecordController {
    @Autowired
    GoodsStorageRecordService goodsStorageRecordService;

    @Autowired
    GoodsSupplierService goodsSupplierService;

    @RequestMapping("/sys/goodsStorageRecord/list")
    public String getAllgoodsStorageRecordlist(@RequestParam(value = "goodsName",required = false) String goodsName,
                                               @RequestParam(value = "goodsCode",required = false) String goodsCode,
                                               @RequestParam(value = "payStatus",required = false) Integer payStatus,
                                               @RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                               HttpServletRequest request){
        //用户
        GoodsStorageRecord goodsStorageRecordCondition = new GoodsStorageRecord();
        if(payStatus!=null){
            goodsStorageRecordCondition.setPayStatus(payStatus);
            //回显模糊查询数据
            request.setAttribute("payStatus",payStatus);
        }
        if(goodsName!=null && !"".equals(goodsName)){
            goodsStorageRecordCondition.setGoodsName(goodsName);
            //回显模糊查询数据
            request.setAttribute("goodsName",goodsName);
        }
        if (goodsCode!=null && !"".equals(goodsCode)){
            goodsStorageRecordCondition.setGoodsCode(goodsCode);
            //回显roleid
            request.setAttribute("goodsCode",goodsCode);
        }
        int pageNum = 1;//默认设置为第一页
        if (pageIndex!=null){
            pageNum = pageIndex;
        }
        PageInfo<GoodsStorageRecord> pageInfo = goodsStorageRecordService.getGoodsStorageRecordByPage(goodsStorageRecordCondition, pageNum);
        request.setAttribute("pageInfo",pageInfo);
        return "goodsStorageRecord/list";

    }

    //商品入库信息管理界面添加商品入库信息功能 1.进入添加页面
    @RequestMapping("/addGoodsStorageRecord")
    public  String addGoodsStorageRecord1(HttpServletRequest request){
        //供货商编号
        List<GoodsSupplier> allcode = goodsSupplierService.getAllSuppliercode();
        //供货商编号下拉框显示的数据
        request.setAttribute("codeList",allcode);
        return "goodsStorageRecord/add";
    }

    //商品入库信息管理界面添加供货商信息功能 2.添加商品入库信息
    @RequestMapping("/sys/goodsStorageRecord/add")
    @ResponseBody
    public String addGoodsStorageRecord2(GoodsStorageRecord goodsStorageRecord,
                                         HttpServletRequest request, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(Contants.USERSESSION);
        goodsStorageRecord.setCreatedUserId(user.getId());
        goodsStorageRecord.setCreateTime(new Date());
        int num =  goodsStorageRecordService.addGoodsStorageRecord(goodsStorageRecord);
        if(num>0){
            //保存成功
            return "<script>alert('新增成功');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }else{
            //保存失败
            return "<script>alert('新增失败');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }
    }

    //商品入库信息管理界面查看商品入库信息功能
    @RequestMapping("/getAllRecord/{id}")
    public String getAllRecord(@PathVariable("id") Integer id,HttpServletRequest request){
        GoodsStorageRecord goodsStorageRecord = goodsStorageRecordService.getAllRecordById(id);
        request.setAttribute("Record",goodsStorageRecord);
        return "goodsStorageRecord/view";
    }

    //商品入库信息管理界面修改商品入库信息功能 1.进入修改页面
    @RequestMapping("/updateRecord/{id}")
    public String updateRecord1(@PathVariable("id") Integer id,HttpServletRequest request){
        //供货商编号
        List<GoodsSupplier> allcode = goodsSupplierService.getAllSuppliercode();
        //供货商编号下拉框显示的数据
        request.setAttribute("codeList",allcode);

        //根据id查询到选择的数据信息
        GoodsStorageRecord goodsStorageRecord = goodsStorageRecordService.getAllRecordById(id);
        //回显到修改页面
        request.setAttribute("Record",goodsStorageRecord);
        return "goodsStorageRecord/update";
    }

    //商品入库信息管理界面修改商品入库信息功能 2.进行修改
    @RequestMapping("/sys/goodsStorageRecord/update")
    @ResponseBody
    public String updateRecord2(GoodsStorageRecord goodsStorageRecord,HttpServletRequest request){
        int num =  goodsStorageRecordService.updateGoodsStorageRecord(goodsStorageRecord);
        if(num>0){
            //保存成功
            return "<script>alert('修改成功');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }else{
            //保存失败
            return "<script>alert('修改失败');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }
    }

    //商品入库信息管理界面删除商品入库信息功能
    @RequestMapping("/deletRecord/{id}")
    @ResponseBody
    public String deletRecord(@PathVariable("id") Integer id, HttpServletRequest request){
        int num = goodsStorageRecordService.deletGoodsStorageRecord(id);
        if(num>0){
            //删除成功
            return "<script>alert('删除成功');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }else{
            //删除失败
            return "<script>alert('删除失败');location.href='"+request.getContextPath()+"/sys/goodsStorageRecord/list'</script>";
        }
    }


}
